﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Bot.Application.Dto;
using Bot.Application.Services;
using Microsoft.AspNetCore.Mvc;

namespace Bot.WebAPI.Controllers
{
    [Route("api/v3/chatbot")]
    [ApiController]
    public class IntentsController : ControllerBase
    {

        private readonly IIntentAppService _app;
        public IntentsController(IIntentAppService app)
        {
            this._app = app;
        }

        [HttpGet]
        [Route("intents/{id:Guid}")]
        public ActionResult<IntentDto> Get([FromRoute] Guid id,[FromQuery] string include)
        {
            IntentDto intent = _app.Get(id, include);
            return Ok(intent);
        }

        [HttpGet]
        [Route("bots/{botId:Guid}/intents")]
        public ActionResult<IEnumerable<IntentDto>> GetList([FromRoute] Guid botId, [FromQuery] string include, [FromQuery] Guid? categoryId, [FromQuery] string keyword)
        {
            IEnumerable<IntentDto> list = _app.GetList( botId,include,categoryId,keyword);
            return Ok(list);
        }

        [HttpPut]
        [Route("intents/{id:Guid}/intents")]
        public ActionResult<IntentDto> Put([FromRoute] Guid id, [FromBody] IntentUpdateDto dto)
        {
            IntentDto intent = _app.Update(id, dto);
            return Ok(intent);
        }

        [HttpPost]
        [Route("bots/{botId:Guid}/intents")]
        public ActionResult<IntentDto> Post([FromRoute] Guid botId, [FromBody] IntentCreateDto dto)
        {
            IntentDto intent = _app.Add(botId, dto);
            return CreatedAtAction(nameof(Get), new { id = intent.Id }, intent);
        }

        [HttpDelete]
        [Route("intents/{id:Guid}")]
        public ActionResult Delete([FromRoute] Guid id)
        {
            _app.Delete(id);
            return NoContent();
        }

        [HttpPost]
        [Route("bots/{botId:Guid}/intents:import")]
        public ActionResult<OperationDto> Import([FromRoute] Guid botId)
        {
            string filePath = "";
            OperationDto operation = _app.Import(botId, filePath);
            return Ok(operation);
        }

        [HttpGet]
        [Route("bots/{botId:Guid}/intents:queryTopScore")]
        public ActionResult<IEnumerable<IntentScoreDto>> QueryTopScore([FromRoute] Guid botId, [FromQuery] string question, [FromQuery] int top=10)
        {
            IEnumerable<IntentScoreDto> list = _app.QueryTopScore(botId, question, top);
            return Ok(list);
        }
    }
}
